"use strict";

const Service = require("egg").Service;

class TagService extends Service {
  async update(tag, id) {
    return await this.ctx.model.Tag.update(tag, {
      where: { tagId: id },
    });
  }

  async destroy(id) {
    return await this.ctx.model.Tag.destroy({
      where: { tagId: id },
    });
  }

  /**
   * 获取博客的标签组
   * @param {*} blogId 博客id
   */
  async getTags(blogId) {
    const { QueryTypes } = this.app.Sequelize;
    const sql = `select t.tag_id, t.name from blog_tag as bt, tag as t 
    where blog_id = ? and t.tag_id = bt.tag_id`;
    return await this.ctx.model.query(sql, {
      replacements: [blogId],
      type: QueryTypes.SELECT,
    });
  }
}

module.exports = TagService;
